package ru.rchervot.sanasar.client.view;

import com.google.gwt.event.dom.client.DoubleClickEvent;
import com.google.gwt.event.dom.client.DoubleClickHandler;
import com.google.gwt.event.dom.client.HasDoubleClickHandlers;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.TabBar;

public abstract class AbstractRecordHeaderView extends Composite
implements RecordHeaderPresenter.Display, HasDoubleClickHandlers {
	
	protected final Grid basePanel = new Grid(1,3);
	protected final TabBar selectOneTabs = new TabBar();
	protected final TabBar selectNoneTabs = new TabBar();
	protected final TabBar selectSeveralTabs = new TabBar();
	private RecordHeaderPresenter presenter;
	
	protected final static int FIRST_TAB = 0;
	
	public AbstractRecordHeaderView() {
		super();
		
		this.initWidget(basePanel);
		this.setStylePrimaryName("AbstractRecordHeaderView");
		
		basePanel.setCellPadding(0);
		basePanel.setCellSpacing(0);
		basePanel.getCellFormatter().setVerticalAlignment(0, 0, 
				HasVerticalAlignment.ALIGN_BOTTOM);
		basePanel.getCellFormatter().setVerticalAlignment(0, 1, 
				HasVerticalAlignment.ALIGN_BOTTOM);
		basePanel.getCellFormatter().setVerticalAlignment(0, 2, 
				HasVerticalAlignment.ALIGN_BOTTOM);
		basePanel.setHeight(Constants.kRecordHeaderHeight + "px");
		basePanel.setWidth("100%");
		
		selectOneTabs.addTab("View");
		selectOneTabs.addTab("Notes");
		selectOneTabs.addTab("Duplicates");
		selectOneTabs.addTab("Viewed");
		selectOneTabs.selectTab(0);
		selectOneTabs.setVisible(false);
		
		selectNoneTabs.addTab("Description");
		selectNoneTabs.selectTab(0);
		selectNoneTabs.setVisible(false);
		
		selectSeveralTabs.addTab("Description");
		selectSeveralTabs.selectTab(0);
		selectSeveralTabs.setVisible(false);
		
		
		
		basePanel.setWidget(0, 0, selectOneTabs);
		basePanel.setWidget(0, 1, selectNoneTabs);
		basePanel.setWidget(0, 2, selectSeveralTabs);
		
		getDefaultTabBar().setVisible(true);
		
	}
	
	@Override
	public void setPresenter(RecordHeaderPresenter presenter) {
		this.presenter = presenter;
	}
	
	@Override
	public void renderSelectedNone() {
		selectOneTabs.setVisible(false);
		selectSeveralTabs.setVisible(false);
		
		selectNoneTabs.setVisible(true);
	}

	@Override
	public void renderSelectedOne() {
		selectSeveralTabs.setVisible(false);
		selectNoneTabs.setVisible(false);
		
		selectOneTabs.setVisible(true);
	}

	@Override
	public void renderSelectedSeveral() {
		selectOneTabs.setVisible(false);
		selectNoneTabs.setVisible(false);
		
		selectSeveralTabs.setVisible(true);
	}
	
	@Override
	public void renderEmptyPage() {
		selectSeveralTabs.setVisible(false);
		selectNoneTabs.setVisible(false);
		selectOneTabs.setVisible(false);
	}
	
	@Override
	public void renderStartPage() {
		renderEmptyPage();
	}
	
	private TabBar getDefaultTabBar() {
		return selectNoneTabs;
	}
	
	@Override
	public HasDoubleClickHandlers getBasePanel() {
		return this;
	}
	
	@Override
	public HandlerRegistration addDoubleClickHandler(DoubleClickHandler handler) {
		return this.addDomHandler(handler, DoubleClickEvent.getType());
	}
}
